package com.zsrt.day20170414.pm.daotest;

import java.util.Scanner;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import  com.zsrt.day20170414.pm.entity.StudentEntity;
import com.zsrt.day20170414.pm.daoimpl.StudentDaoImpl;
import com.zsrt.utils.ArrayUtil;

/**
*@author jzh
*@创建时间 2017年4月14日下午6:37:45
*类说明：
*/
public class StudentDaoTest {
	
	StudentEntity[] students=new StudentEntity[7];
	StudentDaoImpl dao=new StudentDaoImpl();
	Scanner in=new Scanner(System.in);
	
	@Before
	public void Init(){	
		StudentEntity jmj=new StudentEntity();
		jmj.setName("贾梦菊");
		jmj.setAge(23);
		students[0]=jmj;
		StudentEntity zjy=new StudentEntity();
		zjy.setName("张嘉源");
		zjy.setAge(22);
		students[1]=zjy;
		StudentEntity lzh=new StudentEntity();
		lzh.setName("罗智壕");
		lzh.setAge(21);
		students[2]=lzh;
		StudentEntity hxq=new StudentEntity();
		hxq.setName("何小强");
		hxq.setAge(28);
		students[3]=hxq;
		StudentEntity jzh=new StudentEntity();
		jzh.setName("靳志辉");
		jzh.setAge(25);
		students[4]=jzh;
		StudentEntity zc=new StudentEntity();
		zc.setName("周驰");
		zc.setAge(26);
		students[5]=zc;
		StudentEntity zsh=new StudentEntity();
		zsh.setName("曾少华");
		zsh.setAge(30);
		students[6]=zsh;
		//ArrayUtil.printArray(students);
		}
	/**
	 * 增单个个学生	
	 */
	@Test
	public void addStudentTest(){
		System.out.println("输入姓名");
		String name = in.nextLine();
		System.out.println("输入身份证号");
		String idCard = in.nextLine();
		System.out.println("输入年龄");
		int age = in.nextInt();
		System.out.println("输入分数");
		double score = in.nextDouble();
		System.out.println("输入性别");
		String sex = in.next();
		
		StudentEntity newStudent=new StudentEntity();
		
		newStudent.setName(name);
		newStudent.setIdCard(idCard);
		newStudent.setAge(age);
		newStudent.setScore(score);
		newStudent.setSex(sex);
		
		StudentDaoImpl dao=new StudentDaoImpl();
		StudentEntity[] newStudents = dao.addStudent(students, newStudent);
		ArrayUtil.printArray(newStudents);
	}
	/**
	 * 输入的名字是否存在
	 */
	@Test
	public void studentExistOrNotByNameTest(){
		System.out.println("输入名字");
		String name = in.nextLine();
		StudentDaoImpl dao=new StudentDaoImpl();
		int index=dao.studentExistOrNotByName(students, name );
		if(index<0){
			System.out.println("输入的名字不存在");
		}else{
			System.out.println("该学生在数组的第"+index+"个位置");
		}
	}
	/**
	 * 删除单个学生
	 */
	@Test
	public void deleteStudentByNameTest(){
		System.out.println("输入要删除学生的姓名");
		String name = in.nextLine();
		int index=dao.studentExistOrNotByName(students,name);
		if(index<0){
			System.out.println("输入的名字不存在，重新输入");
		}else{
			StudentEntity[] newStudents = dao.deleteStudentByName(students, name);
			ArrayUtil.printArray(newStudents);
		}
	}
	/**
	 * 批量删除
	 */
	@Test
	public void deleteStudentByNamesTest(){
		String[] delStudents=new String[3];
		delStudents[0]=students[0].getName();
		delStudents[1]=students[2].getName();
		delStudents[2]="侯亮平";
		//
		int counter=0;
		for(int index=0;index<delStudents.length;index++){
			String name=delStudents[index];
			int index1=dao.studentExistOrNotByName(students,name);
			if(index1>=0){
				counter++;
			}	
		}
		String[] targetArrays=new String[counter];
		counter=0;
		for(int index=0;index<delStudents.length;index++){
			String name=delStudents[index];
			int index1=dao.studentExistOrNotByName(students,name);
			if(index1>=0){
				targetArrays[counter++]=delStudents[index];
			}	
		}
		
		//ArrayUtil.printArray(delStudents);
		StudentEntity[] newStudents = dao.deleteStudentByNames(students,targetArrays);
		ArrayUtil.printArray(newStudents);
	}
	/**
	 * 更改学生的分数
	 */
	@Test
	public void updateStudentScoreTest(){
		System.out.println("输入要更改分数的学生的姓名");
		String name = in.nextLine();
		int index=0;
		while(true){
			index = dao.studentExistOrNotByName(students, name);
			if(index<0){
				System.out.println("输入的名字不存在，请重新输入");
				name = in.nextLine();
			}else{
				break;
			}	
		}
		System.out.println("要改变为多少分");
		int score = in.nextInt();
		students[index].setScore(score);
		System.out.println(students[index]);
	}
	/**
	 * 查找年年龄大于给定数字的学生个数
	 */
	@Test
	public void findStudentByAgeTest() {
		System.out.println("输入一个年龄标准");
		int age = in.nextInt();
		int counter=0;
		for(int index=0;index<students.length;index++){
			int age1 = students[index].getAge();
			if(age1>age){
				counter++;
			}
		}
		System.out.println("年龄大于"+age+"的学生有"+counter+"个");

	}
	
	@Test
	public void loginTest(){
		System.out.println("输入帐号（手机号码）");
		String phone = in.nextLine();
		phone=phone.trim();
		System.out.println("输入密码");
		String password = in.nextLine();
		password=password.trim();
		int number = dao.login(phone, password);
		switch(number){
			case 1:{
				System.out.println("登陆成功");
				deleteStudentByNameTest();
				break;
			}
			case 0:{
				System.out.println("密码错误");
				break;
			}
			case -1:{
				System.out.println("帐号错误");
				break;
			}
		
		}
		
	}
	
	@After
	public void close(){
		in.close();
	}

}
